/* app02-window.c
 *
 * Copyright 2024 wan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "main-window.h"
#include "../data/product.h"
#include <sqlite3.h>
struct _MainWindow
{
  GtkApplicationWindow  parent_instance;

  /* Template widgets */
  //GtkHeaderBar        *header_bar;
  GtkTreeView *prod_view;
  GtkListStore *prod_store;
  GtkDialog *prod_dialog;
  GtkEntry *prod_id_input;
  GtkEntry *prod_name_input;
  GtkEntry *prod_price_input;
  GtkEntry *prod_num_input;
  GtkEntry *prod_apply_input;
  GtkEntry *prod_address_input;
  GtkButton *prod_add_btn;//新增按钮
  GtkButton *prod_cancel_btn;//取消按钮
  GtkMenu *prod_view_menu;
  GtkMenuItem *prod_menu_update;
  GtkMenuItem *prod_menu_delete;
  GtkNotebook *main_notebook;
};

G_DEFINE_TYPE (MainWindow, main_window, GTK_TYPE_APPLICATION_WINDOW)

static void
main_window_class_init (MainWindowClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  //加载ui
  gtk_widget_class_set_template_from_resource (widget_class, "/a/b/c/main/main-window.ui");
  
  gtk_widget_class_bind_template_child (widget_class, MainWindow, prod_view);
  //2.将组件设置到窗体上
  gtk_widget_class_bind_template_child (widget_class, MainWindow, prod_store);
  gtk_widget_class_bind_template_child (widget_class, MainWindow, prod_dialog);
  gtk_widget_class_bind_template_child (widget_class, MainWindow, prod_id_input);
  gtk_widget_class_bind_template_child (widget_class, MainWindow, prod_name_input);
  gtk_widget_class_bind_template_child (widget_class, MainWindow, prod_num_input);
  gtk_widget_class_bind_template_child (widget_class, MainWindow, prod_price_input);
  gtk_widget_class_bind_template_child (widget_class, MainWindow, prod_apply_input);
  gtk_widget_class_bind_template_child (widget_class, MainWindow, prod_address_input);
  gtk_widget_class_bind_template_child (widget_class, MainWindow, prod_add_btn);
  gtk_widget_class_bind_template_child (widget_class, MainWindow, prod_cancel_btn);
  gtk_widget_class_bind_template_child (widget_class, MainWindow, prod_view_menu);
  gtk_widget_class_bind_template_child (widget_class, MainWindow, prod_menu_update);
  gtk_widget_class_bind_template_child (widget_class, MainWindow, prod_menu_delete);
  gtk_widget_class_bind_template_child (widget_class, MainWindow, main_notebook);
}
GtkEntry *prod_id_input;
GtkListStore *prod_store;
enum {
  PROD_ID=0,
  PROD_NAME,
  PROD_PRICE,
  PROD_NUM,
  PROD_APPLY,
  PROD_ADDRESS,
};

//弹出新增商品的对话框
void prod_add_btn_click(GtkWidget *btn,gpointer data){
  //设置编号输入框不可编辑
  gtk_editable_set_editable (GTK_EDITABLE (prod_id_input), FALSE);
  //显示新增对话框
  gtk_widget_show_all (GTK_WIDGET (data));
}
void prod_cancel_btn_click(GtkWidget *btn,gpointer data){
  gtk_widget_hide (GTK_WIDGET(data));
}
gboolean prod_dialog_hide(GtkWidget *dialog){
  gtk_widget_hide (dialog);
  return TRUE;
}
//弹出菜单
gboolean prod_menu_open(GtkTreeView* view,GdkEventButton* event,gpointer data){
  if(event->button==3){
    GtkMenu *menu = GTK_MENU(data);
    gtk_menu_popup_at_pointer (menu, (GdkEvent*)event);
  }
  return FALSE;
}

void select_menu_update(GtkMenuItem* item,gpointer data){
  //选中treeview中的一行
  GtkTreeSelection * selection =  gtk_tree_view_get_selection (GTK_TREE_VIEW (data));
  GtkTreeIter iter; //类似一行数据
  GtkTreeModel *model;//模型
  if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
    //一定要知道这列值是什么类型
    int id;
    gtk_tree_model_get(model, &iter, 0, &id, -1);//获取这行中的id值
    g_print("Selected value: %d\n", id);
    //TODO 根据这个id去查询对应的商品信息，然后显示在修改界面
  }
}
void load_prod_data(char *sql){
  // 查询数据库
  char ** result;
  int rows;
  int cols;
  int res = prod_query(sql,&result,&rows,&cols);
  if(!res){
    printf("rows=%d,cols=%d\n",rows,cols);
    int count = cols-1;
    GtkTreeIter iter;//行
    char prod_id[10];
    char prod_name[20];
    char  prod_price[10];
    char  prod_num[10];
    char  prod_apply[20];
    char prod_address[50];
    for(int i=0;i<rows;i++){
      gtk_list_store_append (prod_store, &iter);//新增一行
      strcpy(prod_id,result[++count]);
      strcpy(prod_name,result[++count]);
      strcpy(prod_price,result[++count]);
      strcpy(prod_num,result[++count]);
      strcpy(prod_apply,result[++count]);
      strcpy(prod_address,result[++count]);
      printf("id=%s,name=%s,price=%s,num=%s,apply=%s,address=%s\n",prod_id,prod_name,prod_price,prod_num,prod_apply,prod_address);
      gtk_list_store_set(prod_store,&iter,PROD_ID,atoi(prod_id),PROD_NAME,prod_name,PROD_PRICE,atof (prod_price)
                          ,PROD_NUM,atoi(prod_num),PROD_APPLY,prod_apply,PROD_ADDRESS,prod_address,-1);
                             
    } 
    sqlite3_free_table (result);
  }
    
}

void notebook_switch_page(GtkNotebook*notebook,gpointer page,int num,gpointer data){
  if(num==2){
    printf("product.....\n");
    char *sql = "select * from table_product";
    load_prod_data (sql);
  }  
}

static void
main_window_init (MainWindow *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
  prod_id_input = self->prod_id_input;
  prod_store = self->prod_store;
  
  //将list-sotre绑定到tree_view上
  gtk_tree_view_set_model (self->prod_view,GTK_LIST_STORE (self->prod_store));
  //创建数据渲染器
  GtkCellRenderer *renderer;
    renderer = gtk_cell_renderer_text_new();
  
  //创建列
  GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes("id", renderer, "text", 0, NULL);
  //添加列
  gtk_tree_view_append_column(GTK_TREE_VIEW(self->prod_view), column);

  column = gtk_tree_view_column_new_with_attributes("泡面名称", renderer, "text", 1, NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(self->prod_view), column);

  column = gtk_tree_view_column_new_with_attributes("泡面价格", renderer, "text", 2, NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(self->prod_view), column);

  column = gtk_tree_view_column_new_with_attributes("泡面库存", renderer, "text", 3, NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(self->prod_view), column);

  column = gtk_tree_view_column_new_with_attributes("泡面供应商", renderer, "text", 4, NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(self->prod_view), column);

  column = gtk_tree_view_column_new_with_attributes("泡面产地", renderer, "text", 5, NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(self->prod_view), column);
  //默认显示商品页面
  

  //为新增按钮设置信号：结构体的指针
  g_signal_connect (self->prod_add_btn, "clicked", G_CALLBACK(prod_add_btn_click), self->prod_dialog);
  
  //为取消按钮链接单击信号
  g_signal_connect (self->prod_cancel_btn, "clicked", G_CALLBACK(prod_cancel_btn_click), self->prod_dialog);
  //为对话框重写关闭
  g_signal_connect (self->prod_dialog, "delete_event", G_CALLBACK(prod_dialog_hide), NULL);
  
  //为商品列表添加右键快捷菜单
  g_signal_connect (self->prod_view, "button-press-event", G_CALLBACK(prod_menu_open), self->prod_view_menu);
  
  g_signal_connect (self->prod_menu_update, "activate", G_CALLBACK(select_menu_update), self->prod_view);
  
  //导航
  g_signal_connect (self->main_notebook, "switch-page", G_CALLBACK(notebook_switch_page), NULL);
}
